[TOC]

Creating Structure

A structure is a variable that serves as a container of other variables. The basic concept is illustrated in the figure below, where a structure called "car" and its fields are shown.

car

Method 1: Starting with a Field

We create a structure named car that contains the field owner:

Input
car.owner = 'marc'
Output
car =
 struct with field:
 owner: ['marc']

We have just created a structure called car that contains only 1 field, owner. The owner is marc. Now, you add the fields engine, mileage and manufacturer. The field car.engine is actually another structure containing the fields capacity, turbo and diesel. The following code completes the implementation of the structure car.

Input
car.engine.turbo = false;
car.engine.diesel = false;
car.engine.capacity = 1.4;
car.owner = 'marc';
car.mileage = 140000;
car.manufacturer = 'mazda';

% Print out car and car.engine
car
car.engine
Output
car =
 struct with fields:
        owner: ['marc']
 manufacturer: ['mazda']
      mileage: [1.4000]
       engine: Struct [1 x 1]

ans =
 struct with fields:
 capacity: [1.4000]
   diesel: [0]
    turbo: [0]

In the above code, car is a structure (or structure scalar), which contains variables representing its attributes:

These attributes are called the fields of car. Fields of a structure may have different data types. For examples, owner is a character array and mileage is a double. A structure can contain another structure as its field. Here, car has the field engine, which is a structure containing other fields: capacity, turbo and diesel.

A structure array is an array containing structure scalars. In the above example, car is a structure array of 1 element. In the following example, marc_car, joe_car and ann_car are structures having the same fields. They can be catenated to form a structure array of 1 row: [marc_car, joe_car, ann_car].

Input
marc_car.owner = 'marc';
marc_car.engine.turbo = false;
marc_car.engine.capacity = 1.4;
marc_car.engine.diesel = false;
marc_car.mileage = 140000;
marc_car.manufacturer = 'mazda';

joe_car.owner = 'joe';
joe_car.engine.turbo = false;
joe_car.engine.capacity = 1.0;
joe_car.engine.diesel = false;
joe_car.mileage = 40000;
joe_car.manufacturer = 'toyota';

ann_car.owner = 'an';
ann_car.engine.turbo = true;
ann_car.engine.capacity = 2.2;
ann_car.engine.diesel = false;
ann_car.mileage = 3000;
ann_car.manufacturer = 'subaru';

% Catenate structures to form a row.
cars = [marc_car, joe_car, ann_car]
Output
cars =
 struct array (1 x 3) with fields:
 mileage, manufacturer, engine, owner

Method 2: Using Function struct

The second method is to use the built-in function struct. The following shows how the structures engine and car can be implemented. A field name is given as a character vector, followed by the field's value. The values can be any data type including double, logical, character, a structure, or function handle.

Input
engine = struct('capacity', 1.4, 'turbo', false, 'diesel', false)
car = struct('engine', engine, 'owner', 'marc', 'manufacturer', 'mazda', 'mileage', 10456) 
Output
engine =
 struct with fields:
 capacity: [1.4000]
    turbo: [0]
   diesel: [0]

car =
 struct with fields:
       engine: Struct [1 x 1]
      mileage: [10456.]
 manufacturer: ['mazda']
        owner: ['marc']

If no input argument is given, struct creates a structure with no fields:

Input
struct
Output
ans =
 struct with no field

Adding Fields

Structures contained in the same array must satisfy the following requirements:

  1. All structures in the same array must have the same number of fields,
  2. All structures in the same array must have fields with the same field names.

Nevertheless, fields with the same names can contain data of different types and/or sizes. For example, we can add an electric car to the structure array cars. But, the engine fields (capacity, turbo and diesel) are not applicable to an electric motor. Hence, we simply assign the character array electric to the car's engine field:

Input
john_car.mileage = 4000;
john_car.engine = 'electric';
john_car.manufacturer = 'tesla';
john_car.owner = 'john';
cars(4) = john_car
cars(4)
Output
cars =
 struct array (1 x 4) with fields:
 engine, owner, mileage, manufacturer

ans =
 struct with fields:
        owner: ['john']
 manufacturer: ['tesla']
      mileage: [4000.0]
       engine: ['electric']

We can add more fields to a structure array. If we want to specify that a Tesla car is cool, we add the field cool to cars(4):

Input
cars(4).cool = true
Output
cars =
 struct array (1 x 4) with fields:
 engine, owner, mileage, manufacturer, cool

We did not specify whether the other cars are cool or not. Hence, the field cool is an empty array by default for the other cars:

Input
cars(1).cool
cars(2).cool
cars(3).cool
Output
ans =
 [] (double array)

ans =
 [] (double array)

ans =
 [] (double array)